﻿using System.Threading.Tasks;
using Caltech.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Caltech.Results;
using CaltechSoft.Service.CommandService;
using ServiceModel = CaltechSoft.Service.DBCommand;
using AutoMapper;
using CaltechSoft.Service.ReportService;
using CaltechSoft.Service.SearchCriteria;
using CaltechSoft.Service.Repositories;
using CaltechSoft.Service.EntityFW.Entity;
using System.Linq.Expressions;
using LinqKit;
using Microsoft.Owin.Security;
using System.Web.Http.Owin;
using System.Web;
using Caltech.Extensions;
using Caltech.Service.Common;

namespace Caltech.Controllers
{
    [HasAccess(Areas.CustomerEquipmentView)]
    public class CustomerEquipmentController : BaseApiController<CustomerEquipment, CustomerEquipmentModel, CustomerEquipmentSearchModel>
    {
        public CustomerEquipmentController(IRepository<CustomerEquipment> repository)
            : base(repository)
        {

        }

        [HasAccess(Areas.CustomerEquipmentView)]
        public override IHttpActionResult Get()
        {
            var queryItems = Request.RequestUri.ParseQueryString();
            string customerName = queryItems["customerName"];
            int id;

            var predicate = PredicateBuilder.True<CustomerEquipment>();

            if (queryItems["model"] != null)
            {
                var model = queryItems["model"].ToLower();
                predicate = predicate.And(equipment => equipment.Equipment.ModelNumber.ToLower().Contains(model));
            }
            else if (!string.IsNullOrEmpty(customerName))
            {
                customerName = customerName.ToLower();
                predicate = predicate.And(equipment => equipment.Customer.Name.ToLower().Contains(customerName));
            }
            else if (int.TryParse(queryItems["customerId"], out id))
            {
                predicate = predicate.And(equipment => equipment.CustomerId == id);
            }

            if (queryItems["description"] != null)
            {
                var description = queryItems["description"].ToLower();
                predicate = predicate.And(equipment => equipment.Equipment.Name.ToLower().Contains(description.Trim()));
            }

            return QueryData(predicate);
        }

        [HasAccess(Areas.CustomerEquipmentView)]
        public override async Task<IHttpActionResult> GetBy(int id)
        {
            return await base.GetBy(id);
        }

        [HasAccess(Areas.CustomerEquipmentEdit)]
        public override async Task<IHttpActionResult> Put(CustomerEquipmentModel model)
        {
            return await base.Put(model);
        }

        [HasAccess(Areas.CustomerEquipmentCreate)]
        public override async Task<IHttpActionResult> Post(CustomerEquipmentModel model)
        {
            return await base.Post(model);
        }

        [HasAccess(Areas.CustomerEquipmentDelete)]
        public override async Task<IHttpActionResult> Delete(int id)
        {
            return await base.Delete(id);
        }
    }
}
